package com.example.testapplication;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;

public class DialogDemo extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_test);
    }

    /**
     *   显示一般AlertDialog
     * */

    public void showAD(View view){
        new AlertDialog.Builder(this)
                .setTitle("删除数据") // 设置标题
                .setMessage("你确定删除数据吗")
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(DialogDemo.this, "删除数据", Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(DialogDemo.this, "取消删除数据", Toast.LENGTH_SHORT).show();
            }
        }).show(); // 方法链调用
    }

    /**
     *  显示单选列表
     * **/

    public void showLD(View v){
        final String[] items = {"红","蓝","绿","灰"};
        new AlertDialog.Builder(this)
                .setTitle("指定背景色")
                .setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) { // i 选中的position
                        // 提示颜色
                        Toast.makeText(DialogDemo.this, items[i], Toast.LENGTH_SHORT).show();
                        // 移除dilaog
                        dialogInterface.dismiss();
                    }
                }).show();
    }

    /**
     *  自定义AlertDialog
     * */
    public void showCD(View v){
        // 动态加载布局文件，得到对应的View对象
        View view = View.inflate(this,R.layout.dialog_view,null);
        //问题1:　view的真实类型?是布局文件根标签的类型, 包含了子View对象
        //问题2:　如何得到一个独立View的子View?  view.findViewById(id)
        //findViewById(id)是在setContentView()中的View中找
        final EditText nameET = (EditText) view.findViewById(R.id.et_dialog_name);
        final EditText pwdET = (EditText) view.findViewById(R.id.et_dialog_pwd);

        new AlertDialog.Builder(this)
                .setView(view)
                .setNegativeButton("取消",null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                // 读取用户名和密码
                String username = nameET.getText().toString();
                String pwd = pwdET.getText().toString();
                // 提示
                Toast.makeText(DialogDemo.this, username +":" + pwd, Toast.LENGTH_SHORT).show();
            }
        }).show();

    }

    /**
     *  显示圆形进度
     * */

    public void showPD(View v){
        final ProgressDialog dialog = ProgressDialog.show(this,"数据加载","数据加载中....");

        // 模拟做一个长时间的工作
        new Thread(){
            public void run(){
                for(int i =0;i<20;i++){
                    try{
                        Thread.sleep(100);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                // 移除dialog
                dialog.dismiss();

                // 不能在分线程上直接更新UI
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DialogDemo.this,"加载完成了",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();

    }
    /**
     * 显示水平进度ProgressDialog
     * @param v
     */
    public void showPD2(View v) {
        //1. 创建dialog对象
        final ProgressDialog pd = new ProgressDialog(this);
        //2. 设置样式
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //3. 显示
        pd.show();

        //4. 启动分线程, 加载数据, 并显示进度, 当加载完成移除dilaog
        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = 20;
                //设置最大进度
                pd.setMax(count);
                for(int i=0;i<count;i++) {
                    //休息一会
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pd.setProgress(pd.getProgress()+1);
                }
                //移除dialog
                pd.dismiss();
            }
        }).start();
    }

    /**
     * 年月日选择框
     * */
    public void showDateAD(View view){
        // 创建日历对象
        Calendar calendar = Calendar.getInstance();
        // 得到当前年月日
        int year = calendar.get(Calendar.YEAR);  // 年
        int month = calendar.get(Calendar.MONTH);  // 月
        int day = calendar.get(Calendar.DAY_OF_MONTH); // 得到日

        Log.e("TAG",year +"-"+month+"-"+day);
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int month,
                                  int day) {
                Log.e("TAG", year+"--"+(month+1)+"--"+day);
            }
        }, year, month, day).show();
    }

    /**
     * 时间选择
     * */
    public void showTimeAD(View v){
        // 问题1 这里如何设置为中国北京时间
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY); // 得到小时
        int minute = c.get(Calendar.MINUTE); // 得到分钟
        Log.e("TAG",hour+":" +minute);
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hour, int minute) {

                Log.e("TAG",hour+":"+minute);
            }
        },hour,minute,true).show();
    }

}
